

package com.gc.server.service.impl;
import com.gc.server.entity.SchoolEntity;
import com.gc.server.entity.ShopEntity;
import com.yf.response.RRException;
import org.springframework.transaction.annotation.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import  com.gc.server.vo.down.RouteDown;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import java.net.URLEncoder;
import com.gc.server.vo.down.RouteDown;
import javax.servlet.http.HttpServletResponse;
import java.util.stream.Collectors;
import com.gc.server.dao.RouteDao;
import com.gc.server.entity.RouteEntity;
import com.gc.server.vo.RouteVo;
import com.gc.server.service.RouteService;
import com.yf.service.impl.BaseServiceImpl;

/**
 * 账户记录Service实现类
 *
 * @author hxf
 * @email 505860922@qq.com
 * @date Fri Oct 16 18:16:41 CST 2020
 */
@Service
public class RouteServiceImpl extends BaseServiceImpl<RouteEntity> implements RouteService {

    private RouteDao dao;


    @Autowired
    public void setDao(RouteDao dao) {
        this.dao = dao;
        super.setBaseDao(dao);
    }




    @Override
    public List<RouteVo> findVo(RouteVo vo){
        return dao.findVo(vo);
    }

    @Override
    public RouteVo getVo(Long id){
        return dao.getVo(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int saveVo(RouteVo vo){
        return dao.save(vo.getEntity());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateVo(RouteVo vo){
        return dao.update(vo.getEntity());
    }



    @Override
    public void downLoad(HttpServletResponse response, List<RouteVo> list) {
        try {
            List<RouteDown> datas = list.stream().map(item -> new RouteDown(item)).collect(Collectors.toList());
            response.setContentType("application/vnd.ms-excel" );
            response.setCharacterEncoding("utf-8" );
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("测试", "UTF-8" );
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx" );
            EasyExcel.write(response.getOutputStream(), RouteDown.class).sheet("模板" ).doWrite(datas);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }




    /**内部方法**/



    /**外部方法实现**/


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(Long userId,RouteEntity routeEntity) {
        if(routeEntity.getName()==null||routeEntity.getName().equals("")){
            throw new RRException("请填写路线名字");
        }
        //新增
        if(routeEntity.getId()==null){
            routeEntity.setUserId(userId);
            dao.save(routeEntity);
            if(routeEntity.getShopIds()==null||routeEntity.getSchoolIds()==null){
                throw new RRException("请选择药店或者学校");
            }
        }else { //修改
            dao.update(routeEntity);
            //先删除所有的
            dao.rmShop(routeEntity.getId());
            dao.rmSchool(routeEntity.getId());
        }
        //添加药店
        if(routeEntity.getShopIds()!=null){
            List<Long> shopIds = routeEntity.getShopIds().stream().filter(s -> s!=null).distinct().collect(Collectors.toList());
            if(shopIds.size()>0){
                dao.addShop(routeEntity.getId(),shopIds);
            }
        }
        //添加学校
        if(routeEntity.getSchoolIds()!=null){
            List<Long> schoolIds = routeEntity.getSchoolIds().stream().filter(s -> s!=null).distinct().collect(Collectors.toList());
           if(schoolIds.size()>0){
               dao.addSchool(routeEntity.getId(),schoolIds);
           }
        }

    }

    @Override
    public List<RouteEntity> findByUserId(Long userId) {
        return dao.findByUserId(userId);
    }

    @Override
    public List<SchoolEntity> findSchool(Long userId) {
        return dao.findSchoolByUserId(userId);
    }

    @Override
    public List<ShopEntity> findShop(Long userId) {
        return dao.findShopByUserId(userId);
    }
}
